package com.gss.armory.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gss.armory.form.DocReports;
import com.gss.armory.form.DocumentCustodianDetailsForm;
import com.gss.armory.form.ReportsForm;
import com.gss.armory.persistence.ReportsDAO;
import com.gss.common.util.ApplicationMaps;

@Service
public class ReportingService {

	private Logger logger = Logger.getLogger(ReportingService.class);

	@Autowired
	ReportsDAO reportsDAO;

	public List fetchReports(ReportsForm reportsForm) {

		List result = null;
		try {
			result = reportsDAO.returnReports(reportsForm);
		} catch (Exception e) {
			//e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
		return result;
	}

	public List<DocReports> fetchExpiredDocReports(DocReports docReports) {
	
		List<Object[]> resultList = null;
		try {
			resultList = reportsDAO.returnExpiredDocReports(docReports);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		List<DocReports> resultDTOList = new ArrayList<DocReports>();
		if(null != resultList){
		for(Object[] resultRow : resultList){
			DocReports resultDTO = new DocReports();
			resultDTO.setDocCode((String) resultRow[0]);
			resultDTO.setDocName((String) resultRow[1]);
			resultDTO.setDocTypeId((Integer) resultRow[2]);
			resultDTO.setDocCategoryId((Integer) resultRow[3]);
			resultDTO.setDocSubCategoryId((Integer) resultRow[4]);
			resultDTO.setStrCreatedAuthor((String) resultRow[5]);
			resultDTO.setDocOwnerName((String) resultRow[6]);
			resultDTO.setDocDepttId(((BigInteger) resultRow[7]).longValue());
			resultDTO.setDocLocId(((BigInteger) resultRow[8]).longValue());
			resultDTO.setDocValidityStartDate((Date) resultRow[9]);
			resultDTO.setDocValidityEndDate((Date) resultRow[10]);
			resultDTO.setAuthSignId((String) resultRow[11]);
			resultDTO.setDocId(((BigInteger) resultRow[12]).longValue());
			resultDTO.setDocIssuingAuthority((String) resultRow[13]);
			resultDTO.setDocEnteredWith((String) resultRow[14]);
			resultDTO.setDocCompanyId((Integer)resultRow[15]);
			
			
			if(resultDTOList.contains(resultDTO))
			{
				DocReports docReports2 = new DocReports();
				docReports2 =resultDTOList.get(resultDTOList.indexOf(resultDTO));
				if(resultDTO.getAuthSignId()!=null&&docReports2.getAuthSignId()!=null&&!docReports2.getAuthSignId().equalsIgnoreCase(resultDTO.getAuthSignId()))
					docReports2.setAuthSign(docReports2.getAuthSign()+','+ApplicationMaps.USERID_USERNAME_MAP.get(resultDTO.getAuthSignId()));
			}
			else
				resultDTOList.add(resultDTO);
		}
		}
		
		logger.debug("Number of results : " + resultDTOList.size());
		
		return resultDTOList;
	}

	public List<DocReports> fetchNearExpiryDocReports(DocReports docReports) {

		List<Object[]> resultList = null;
		try {
			resultList = reportsDAO.returnNearExpiryDocReports(docReports);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		List<DocReports> resultDTOList = new ArrayList<DocReports>();
		if(null == resultList)
			return resultDTOList;
		
		for(Object[] resultRow : resultList){
			DocReports resultDTO = new DocReports();
			resultDTO.setDocCode((String) resultRow[0]);
			resultDTO.setDocName((String) resultRow[1]);
			resultDTO.setDocTypeId((Integer) resultRow[2]);
			resultDTO.setDocCategoryId((Integer) resultRow[3]);
			resultDTO.setDocSubCategoryId((Integer) resultRow[4]);
			resultDTO.setStrCreatedAuthor((String) resultRow[5]);
			resultDTO.setDocOwnerName((String) resultRow[6]);
			resultDTO.setDocDepttId(((BigInteger) resultRow[7]).longValue());
			resultDTO.setDocLocId(((BigInteger) resultRow[8]).longValue());
			resultDTO.setDocValidityStartDate((Date) resultRow[9]);
			resultDTO.setDocValidityEndDate((Date) resultRow[10]);
			resultDTO.setAuthSignId((String) resultRow[11]);
			resultDTO.setDocId(((BigInteger) resultRow[12]).longValue());
			resultDTO.setDocIssuingAuthority((String) resultRow[13]);
			resultDTO.setDocEnteredWith((String) resultRow[14]);
			resultDTO.setDocCompanyId((Integer) resultRow[15]);
			
			String masterCode = ((Integer) resultRow[2]) + "#" + (((BigInteger) resultRow[8]).longValue()) + "#" + (((BigInteger) resultRow[7]).longValue());
			
			if(ApplicationMaps.ARMORY_REVIEVER_APPROVER.containsKey(masterCode) && null != ApplicationMaps.ARMORY_REVIEVER_APPROVER.get(masterCode)){
				
				resultDTO.setApprover(ApplicationMaps.USERID_USERNAME_MAP.get(ApplicationMaps.ARMORY_REVIEVER_APPROVER.get(masterCode).getApprover()));
			}
			
			if(resultDTOList.contains(resultDTO))
			{
				DocReports docReports2 = new DocReports();
				docReports2 =resultDTOList.get(resultDTOList.indexOf(resultDTO));
				if(resultDTO.getAuthSignId()!=null&&docReports2.getAuthSignId()!=null&&!docReports2.getAuthSignId().equalsIgnoreCase(resultDTO.getAuthSignId()))
					docReports2.setAuthSign(docReports2.getAuthSign()+','+ApplicationMaps.USERID_USERNAME_MAP.get(resultDTO.getAuthSignId()));
			}
			else
				resultDTOList.add(resultDTO);
		}
		
		logger.debug("Number of results : " + resultList.size());
		
		return resultDTOList;
	}

	public List<DocumentCustodianDetailsForm> fetchVaultDetailsReports(
			DocumentCustodianDetailsForm detailsForm) {

		List<Object[]> resultList = null;
		try {
			resultList = reportsDAO.returnVaultDetailsReports(detailsForm);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		List<DocumentCustodianDetailsForm> resultDTOList = new ArrayList<DocumentCustodianDetailsForm>();
		if(null == resultList)
			return resultDTOList;
		
		for(Object[] resultRow : resultList){
			
			DocumentCustodianDetailsForm resultDTO = new DocumentCustodianDetailsForm();
			
			if(null != resultRow[0])
				resultDTO.setDocCode((String) resultRow[0]);
			if(null != resultRow[1])
				resultDTO.setDocName((String) resultRow[1]);
			if(null != resultRow[2])
				resultDTO.setVaultNumber((String)resultRow[2]);
			if(null != resultRow[3])
				resultDTO.setVaultLocation((String) resultRow[3]);
			if(null != resultRow[4])
				resultDTO.setCustodianName((String) resultRow[4]);
			if(null != resultRow[5])
				resultDTO.setCustodianDesig((String) resultRow[5]);
			if(null != resultRow[6])
				resultDTO.setCustodianLocId(((BigInteger)resultRow[6]).longValue());
			if(null != resultRow[7])
				resultDTO.setCustodianDepttId(((BigInteger)resultRow[7]).longValue());
			if(null != resultRow[8])
				resultDTO.setCustodianId(resultRow[8].toString());
			if(null != resultRow[9])
				resultDTO.setDocTypeId((Integer) resultRow[9]);
			if(null != resultRow[10])
				resultDTO.setDocCategoryId((Integer) resultRow[10]);
			if(null != resultRow[11])
				resultDTO.setDocSubCategoryId((Integer) resultRow[11]);
			if(null != resultRow[12])
				resultDTO.setDocId(((BigInteger)resultRow[12]).longValue());
			
			resultDTO.setDocCompanyId((Integer) resultRow[13]);
			
			resultDTOList.add(resultDTO);
		}
		
		logger.debug("Number of results : " + resultDTOList.size());
		
		return resultDTOList;
	}

}
